<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>多条件判断</title>
</head>

<body>

</body>
<script>
  "use strict"; {
    let Statistics = function () {
      console.log('执行')
    };
    let currentTab = 0;
    switch (currentTab) {
      case 0:
        Statistics();
        break;
      case 1:
        Statistics();
        break;
      case 2:
        Statistics();
        break;
      case 3:
        Statistics();
        break;
    };
  }; {
    // 新写法了
    var Statistics = function () {
      console.log('执行')
    }
    const comparativeTotles = new Map([
      [0, Statistics],
      [1, Statistics],
      [2, Statistics],
      [3, Statistics]
    ])
    let map = function (val) {
      return comparativeTotles.get(val)
    };
    let getMap = map(1); //如果查找不到返回undefined
    if (!getMap) {
      console.log('查找不到')
    } else {
      console.log('执行操作')
      getMap()
    };
  }; {
    // 对新写法再次优化吧
    const a = () => {
      console.log(`我是a的了吧`);
    };
    const b = () => {
      console.log(`bbb == bbb`);
    };
    const c = () => {
      console.log(`ccc == ccc`);
    };
    // 此方法存在一个问题，当index为3时出现报错
    const test = (index = 1) => {
      return new Map([
        [0, a],
        [1, b],
        [2, c]
      ]).get(index)();
    };
    test();

    // 当index为3时出现报错，也可以改成使用 ?. 来调用函数
    let index = 5
    new Map([
      [0, a],
      [1, b],
      [2, c]
    ]).get(index)?.()
  }; {
    let t1 = () => {
      console.log(`我是t1的哈哈`)
    }
    let t2 = () => {
      console.log(`t2`)
    }
    let t3 = () => {
      console.log(`t3`)
    }
    let anything = 1
    // 这样子，即使anything = 4时也不会报错了
    let data = {
      1: t1,
      2: t2,
      3: t3
    }
    data[anything] && data[anything]()
    // 或者使用?.
    let someThing = {
      1: t1,
      2: t2,
      3: t3
    }[anything]
    someThing?.()
  }; {
    // 复杂分支优化
    function getUser(name) {
      if (name.length > 3) {
        console.log(`名字太长了`);
      } else if (name.length < 2) {
        console.log(`名字太短了`);
      } else if (name[0] === `陈`) {
        console.log(`小陈`);
      } else if (name[0] === `李` && name !== `李鹏`) {
        console.log(`小李`);
      } else if (name === `李鹏`) {
        console.log(`管理员`);
      } else {
        console.log(`此人很神秘`);
      };
    };

    // 引入二维数组进行分支优化
    function getUser1(name) {
      // 声明一个 “管理的” Map对象
      const descriNameMap = [
        [
          (name) => name.length > 3,
          () => console.log(`名字太长了`)
        ],
        [
          (name) => name.length < 2,
          () => console.log(`名字太短了`)
        ],
        [
          (name) => name[0] === `陈`,
          () => console.log(`小陈`)
        ],
        [
          (name) => name === `李鹏`,
          () => console.log(`管理员`)
        ],
        [
          (name) => name[0] === `李` && name !== `李鹏`,
          () => console.log(`小李`)
        ],
      ];
      // 通过 find 函数查找到对象内容，再执行里面的函数了
      const getDesc = descriNameMap.find(v => v?.[0]?.(name));
      getDesc?.[1]?.();
    };

    getUser1("李白");
  };
</script>

</html>